from django.core.paginator import Paginator
from django.db.models import QuerySet
from django.http import HttpResponse, QueryDict
from django.views import View
from django.shortcuts import render

import article.models as models


def HomeView(request):
    params: QueryDict
    params = request.GET
    user_id = request.session.get('user_id', '')
    article_category_id = params.get("article_category_id")
    start_time = params.get("start_time", "")
    end_time = params.get("end_time", "")
    article_title = params.get("article_title", "")
    article_category = models.ArticleCategory.objects.all().filter(create_user=user_id)
    articles_query_set = models.Article.objects.all().filter(create_user=user_id)
    articles = []
    if article_title != "":
        articles_query_set = articles_query_set.filter(article_title__contains=article_title)
    if article_category_id != "" and article_category_id != "0":
        articles_query_set = articles_query_set.filter(category_id=article_category_id)
    if start_time != "" and end_time != "":
        articles_query_set = articles_query_set.filter(publish_time__range=(start_time, end_time))
    # 查询articles、article_category数据
    if user_id:
        for i in articles_query_set:
            # 通过articles的id查询comments的总数
            comments_count = models.Comments.objects.filter(article_id=i.id).count()
            # 给articles_query_set中的每个对象添加comment属性
            i.comment = comments_count
            articles.append(i)
    # 获取当前页
    page_index = request.GET.get("page_index", "1")
    # 创建Paginator分页类
    paginator = Paginator(articles, 5)
    # 管理当前页数据
    current_page = paginator.page(int(page_index))
    return render(request, 'manage/home.html', locals())

class ManageHomeView(View):
    def get(self, request):
        user_id = request.session.get('user_id', '')
        article_category = models.ArticleCategory.objects.all().filter(create_user=user_id)
        articles = []
        # 查询articles、article_category数据
        if user_id:
            articles_query_set = models.Article.objects.all().filter(create_user=user_id)
            for i in articles_query_set:
                # 通过articles的id查询comments的总数
                comments_count = models.Comments.objects.filter(article_id=i.id).count()
                # 给articles_query_set中的每个对象添加comment属性
                i.comment = comments_count
                articles.append(i)
        # 获取当前页
        page_index = request.GET.get("page_index", "1")
        # 创建Paginator分页类
        paginator = Paginator(articles,5)
        # 管理当前页数据
        current_page = paginator.page(int(page_index))

        return render(request, 'manage/home.html', locals())
    def post(self, request):
        params: QueryDict
        params = request.POST
        article_title = params.get("article_title", "")
        article_category_id = params.get("article_category_id", "0")
        start_time = params.get("start_time", "")
        end_time = params.get("end_time", "")
        user_id = request.session.get('user_id', '')
        article_category = models.ArticleCategory.objects.all().filter(create_user_id=user_id)
        articles_query_set = models.Article.objects.all()
        if article_title != "":
            articles_query_set = articles_query_set.filter(article_title__contains=article_title)
        if article_category_id != "" and article_category_id != "0":
            articles_query_set = articles_query_set.filter(category_id=article_category_id)
        if start_time != "" and end_time != "":
            articles_query_set = articles_query_set.filter(publish_time__range=(start_time, end_time))
        articles = []
        for i in articles_query_set:
            # 通过articles的id查询comments的总数
            comments_count = models.Comments.objects.filter(article_id=i.id).count()
            # 给articles_query_set中的每个对象添加comment属性
            i.comment = comments_count
            articles.append(i)

        # 分页逻辑
        paginator = Paginator(articles, 10)  # 每页10条
        page = request.GET.get("page_index", "1")
        return render(request, 'manage/home.html', locals())


class ManageAddArticleView(View):
    def get(self, request):
        article_category = models.ArticleCategory.objects.all()
        return render(request, 'manage/article/article_add.html', locals())

    def post(self, request):
        params: QueryDict
        params = request.POST
        article_title = params.get("title", "")
        article_intro = params.get("intro", "")
        article_content = params.get("content", "")
        article_link = params.get("link", "")
        article_category_id = params.get("category_id", "0")
        file = request.FILES.get("file", "")
        user_id = request.session.get('user_id', '')
        models.Article(article_title=article_title,
                       article_intro=article_intro,
                       article_content=article_content,
                       address_link=article_link,
                       category_id=article_category_id,
                       create_user_id=user_id,
                       update_user_id=user_id,
                       article_img=file).save()
        return HttpResponse("添加成功，<a href='/manage/'>点击返回首页</a>")


class ManageUpdateArticleView(View):
    def get(self, request, id):
        article_category = models.ArticleCategory.objects.all()
        article = models.Article.objects.get(id=id)
        return render(request, 'manage/article/article_update.html', locals())
    def post(self, request, id):
        params: QueryDict
        params = request.POST
        article_title = params.get("title", "")
        article_intro = params.get("intro", '')
        article_content = params.get("content", "")
        article_link = params.get("link", "")
        article_category_id = params.get("category_id", "0")
        file = request.FILES.get("file", "")
        user_id = request.session.get('user_id','')
        models.Article.objects.filter(id=id).update(article_title=article_title,
                                                    article_intro=article_intro,
                                                    article_content=article_content,
                                                    address_link=article_link,
                                                    category_id=article_category_id,
                                                    update_user_id=user_id,
                                                    article_img=file)
        return HttpResponse("修改成功，<a href='/manage/'>点击返回首页</a>")


class ManageDeleteArticleView(View):
    def get(self, request, id):
        models.Article.objects.filter(id=id).delete()
        return HttpResponse("删除成功，<a href='/manage/'>点击返回首页</a>")


class ManageBatchDelArticleView(View):
    def get(self, request):
        param: QueryDict
        param = request.GET
        # 获取ids参数列表
        ids = param.getlist("ids", "")
        if len(ids) != 0:
            models.Article.objects.filter(id__in=ids).delete()
            return HttpResponse("批量删除成功，<a href='/manage/'>点击返回首页</a>")
        return  HttpResponse("批量删除失败，未选中数据，<a href='/manage/'>点击返回首页</a>")
